package Test;

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ControlLayer.RoomCtr;
import ControlLayer.RoomTypeCtr;
import ModelLayer.KeyWord;
import ModelLayer.Room;
import ModelLayer.RoomType;

public class RoomCtrTest {
	private RoomCtr roomCtr;
	private RoomTypeCtr rtCtr;
	private Room room1;
	private Room room2;
	private RoomType rt;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		roomCtr = new RoomCtr();
		rtCtr = new RoomTypeCtr();
		room1 = new Room();
		room2 = new Room();
		rt = new RoomType();
		room1.setName("105");
		room1.setStatus(KeyWord.Available);
		room2.setName("105");
		room2.setStatus(KeyWord.Available);
		rt.setName(KeyWord.DoubleRoom);
		rt.setPricePerNight(100);
		rt.setSize(2);

	}

	@After
	public void tearDown() throws Exception {

	}

	@Test
	public void testInsertAndGetRoom() {
		int roomId = roomCtr.insertRoom("104", 1, "Available");
		Assert.assertEquals(roomId, roomCtr.getRoom(roomId).getId());
		Assert.assertEquals("104", roomCtr.getRoom(roomId).getName());
		Assert.assertEquals(1, roomCtr.getRoom(roomId).getRoomType().getId());
		Assert.assertEquals("Available", roomCtr.getRoom(roomId).getStatus()
				.toString());
		roomCtr.deleteRoom(roomId);
	}

	@Test
	public void testUpdateRoom() {
		int rtId = rtCtr.insertRoomType(rt.getName().toString(),
				rt.getPricePerNight(), rt.getSize());
		room1.setRoomType(new RoomType(rtId));
		int room1Id = roomCtr.insertRoom(room1.getName(), room1.getRoomType()
				.getId(), room1.getStatus().toString());
		Assert.assertEquals(room1Id, roomCtr.getRoom(room1Id).getId());
		Assert.assertEquals("105", roomCtr.getRoom(room1Id).getName());
		Assert.assertEquals(rtId, roomCtr.getRoom(room1Id).getRoomType()
				.getId());
		Assert.assertEquals("Available", roomCtr.getRoom(room1Id).getStatus()
				.toString());

		roomCtr.updateRoom(room1Id, "106", Calendar.getInstance().getTime(),
				rtId, "Occupied");
		Room room = roomCtr.getRoom(room1Id);
		Assert.assertEquals(room1Id, room.getId());
		Assert.assertEquals("106", room.getName());
		Assert.assertEquals(rtId, room.getRoomType().getId());
		Assert.assertEquals("Occupied", room.getStatus().toString());
		Assert.assertEquals(new SimpleDateFormat("dd/MM/yyyy").format(Calendar
				.getInstance().getTime()), new SimpleDateFormat("dd/MM/yyyy")
				.format(room.getEndDate()));

		roomCtr.deleteRoom(room1Id);
		rtCtr.deleteRoomType(rtId);
	}

	@Test
	public void testDeleteRoom() {
		int roomId = roomCtr.insertRoom("104", 1, "Available");
		Assert.assertEquals(1, roomCtr.deleteRoom(roomId));

	}

	@Test
	public void testGetAvailableRoomsNrForRT() {
		int rtId = rtCtr.insertRoomType(rt.getName().toString(),
				rt.getPricePerNight(), rt.getSize());
		room1.setRoomType(new RoomType(rtId));
		room2.setRoomType(new RoomType(rtId));
		int room1Id = roomCtr.insertRoom(room1.getName(), room1.getRoomType()
				.getId(), room1.getStatus().toString());
		int room2Id = roomCtr.insertRoom(room2.getName(), room2.getRoomType()
				.getId(), room2.getStatus().toString());
		Assert.assertEquals(2, roomCtr.getAvailableRoomsNrForRT(rtId));
		roomCtr.deleteRoom(room1Id);
		roomCtr.deleteRoom(room2Id);
		rtCtr.deleteRoomType(rtId);
	}

}
